package com.tom.architect02.zframework.sandbox.base;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Environment;

import androidx.annotation.RequiresApi;

import com.tom.architect02.zframework.sandbox.impl.FileStoreImpl;
import com.tom.architect02.zframework.sandbox.impl.IFile;
import com.tom.architect02.zframework.sandbox.impl.MediaStoreAccessImp;


public class FileAccessFactory {
    @RequiresApi(api = Build.VERSION_CODES.Q)
    public static IFile getIFile(BaseRequest baseRequest) {
//        true  File  执行
        if (!Environment.isExternalStorageLegacy()) {
            //Android 11版本
            setFileType(baseRequest);
            return MediaStoreAccessImp.getInstance();
        } else {
            return new FileStoreImpl();
        }
    }

    // 根据request后缀名，设置文件类型。
    private static void setFileType(BaseRequest request) {
        if (request.getFile().getAbsolutePath().endsWith(".mp3") ||
                request.getFile().getAbsolutePath().endsWith(".wav")) {
            request.setType(MediaStoreAccessImp.AUDIO);
        }else if(request.getFile().getAbsolutePath().startsWith(MediaStoreAccessImp.VIDEO)
                ||request.getFile().getAbsolutePath().endsWith(".mp4")
                || request.getFile().getAbsolutePath().endsWith(".rmvb")
                ||request.getFile().getAbsolutePath().endsWith(".avi")){
            request.setType(MediaStoreAccessImp.VIDEO);
        }else if(request.getFile().getAbsolutePath().startsWith(MediaStoreAccessImp.IMAGE)
                || request.getFile().getAbsolutePath().endsWith(".jpg")
                ||request.getFile().getAbsolutePath().endsWith(".png")){
            request.setType(MediaStoreAccessImp.IMAGE);
        }else {
            request.setType(MediaStoreAccessImp.DOWNLOADS);
        }
    }
}
